package com.sixmac.platform;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.model.ClienUser;
import com.sixmac.service.ClienUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * ClienUserController
 * 2018-07-06 17:49:50
 *
 * @author Blade
 */
@Controller
@RequestMapping("/clienUser")
public class ClienUserController extends BaseController {
    private static String CODE = "clienUser";
    private static String PREFIX = "t_user";
    private static String LIST_SOURCE = "clienUser.list";
    private static String BASE_PATH = "/platform/clienUser/";

    @RequestMapping("/address")
    @ResponseBody
    public AjaxResult parseAddress(Integer id) {
        List<Map> mapList = service.inputFile();
        return json(mapList);
    }


    @Autowired
    ClienUserService service;

    @RequestMapping(KEY_MAIN)
    public String index(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "clienUser.html";
    }

    @RequestMapping(KEY_ADD)
    public String add(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "clienUser_add.html";
    }

    @RequestMapping(KEY_EDIT + "/{id}")
    public String edit(@PathVariable Integer id, ModelMap mm, String addressName) {
        ClienUser clienUser = service.findById(id);
        mm.put("model", JsonKit.toJson(clienUser));
        mm.put("id", id);
        mm.put("code", CODE);
        mm.put("areaId", clienUser.getAddress());
        return BASE_PATH + "clienUser_edit.html";
    }

    @RequestMapping(KEY_VIEW + "/{id}")
    public String view(@PathVariable Integer id, ModelMap mm) {
        ClienUser clienUser = service.findById(id);
        mm.put("model", JsonKit.toJson(clienUser));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "clienUser_view.html";
    }

    @ResponseBody
    @RequestMapping(KEY_LIST)
    public Object list() {
        Object grid = paginate(LIST_SOURCE);
        return grid;
    }

    @ResponseBody
    @RequestMapping(KEY_SAVE)
    public AjaxResult save(String addressName) {
        ClienUser clienUser = mapping(PREFIX, ClienUser.class);
        clienUser.setAddressName(addressName);
        if (clienUser.getStudentId() == null) {
            return error("学号没输入");
        }
        Map m = Db.selectOne("select * from t_user where studentId = #{studentId}", getCMap().put("studentId", clienUser.getStudentId()));
        if (m != null) {
            return error("学号已存在");
        }
        boolean temp = service.save(clienUser);
        if (temp) {
            return success(SAVE_SUCCESS_MSG);
        } else {
            return error(SAVE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_UPDATE)
    public AjaxResult update(String addressName) {
        ClienUser clienUser = mapping(PREFIX, ClienUser.class);
        clienUser.setAddressName(addressName);
        Map m = Db.selectOne("select * from t_user where studentId = #{studentId}", getCMap().put("studentId", clienUser.getStudentId()));
        if (m != null) {
            return error("学号已存在");
        }
        boolean temp = service.update(clienUser);
        if (temp) {
            return success(UPDATE_SUCCESS_MSG);
        } else {
            return error(UPDATE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_REMOVE)
    public AjaxResult remove(@RequestParam String ids) {
        int cnt = service.deleteByIds(ids);
        if (cnt > 0) {
            return success(DEL_SUCCESS_MSG);
        } else {
            return error(DEL_FAIL_MSG);
        }
    }

    public static void main(String[] args) {
    }
}
